﻿using System;
using System.Linq;
using System.Web.Caching;
using System.Xml.Linq;
using Netkk.Common;
using Netkk.Common.Interfaces.Repositories;
using Netkk.Common.Models;
using System.Collections.Generic;

namespace Netkk.Data.Repositories
{
    public class ContentRepository : BaseRepository, IContentRepository
    {
        private string contentPath;

        protected XDocument ContentDocument
        {
            get { return CacheWrapper.Load("content", new CacheDependency(contentPath), () => XDocument.Load(contentPath)); }
        }

        public ContentRepository()
        {
            contentPath = string.Concat(ConnectionString, "Content.xml");
        }

        public IEnumerable<Content> Select()
        {
            IEnumerable<Content> contents = from c in ContentDocument.Element("Contents").Elements("Content")
                                            select getContentFromXElement(c);
            return contents;
        }

        private Content getContentFromXElement(XElement XContent)
        {
            return new Content
            {
                Name = XContent.Element("Name").Value,
                PageTitle = XContent.Element("PageTitle").Value,
                PageDescription = XContent.Element("PageDescription").Value,
                PageKeywords = XContent.Element("PageKeywords").Value,
                PageContent = XContent.Element("PageContent").Value
            };
        }

        public void Delete(Content content)
        {
            XElement xcontent = (from c in ContentDocument.Element("Contents").Elements("Content")
                                 where c.Element("Name").Value.Equals(content.Name, StringComparison.InvariantCultureIgnoreCase)
                                 select c).FirstOrDefault();
            if (xcontent != null)
            {
                xcontent.Remove();
                ContentDocument.Save(contentPath);
            }
        }

        public void Save(Content content)
        {
            XElement xcontent = (from c in ContentDocument.Element("Contents").Elements("Content")
                                 where c.Element("Name").Value.Equals(content.Name, StringComparison.InvariantCultureIgnoreCase)
                                 select c).FirstOrDefault();
            if (xcontent == null)
            {
                xcontent = new XElement("Content");
                ContentDocument.Element("Contents").Add(xcontent);
            }
            else
            {
                xcontent.RemoveAll();
            }

            xcontent.Add(
                new XElement("Name") { Value = content.Name },
                new XElement("PageTitle") { Value = content.PageTitle },
                new XElement("PageDescription") { Value = content.PageDescription },
                new XElement("PageKeywords") { Value = content.PageKeywords },
                new XElement("PageContent") { Value = content.PageContent });

            ContentDocument.Save(contentPath);
        }

    }

}
